<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/css/style.css" type="text/css" media="all"/>
<script src="<?php echo URL::base() ?>media/js/jquery-1.10.1.min.js"></script>

<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css"
      media="all"/>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>

<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<style type="text/css">
    .display_div {
        display: none
    }

    .table thead tr th, .table tbody tr td {
        text-align: center;
    }

    .table thead tr th {
        font-weight: normal;
    }

    .popover {
        width: 200px;
        top: 100%;
        left: -160px;
    }

    .popover.bottom .arrow {
        left: 90%;
    }
</style>
<script type="text/javascript">

    var globe_dialog = {};
    /**
     * 显示详细的信息
     */
    function show_purchase_one_sku(view) {
        art.dialog({
            title: '采购单详情',
            lock: true,
            opacity: 0.87,	// 透明度
            content: view,
            button: [
                {
                    name: '关闭',
                    callback: function () {
                        window.location.reload();

                    }}
            ]
        });
    }
    /**
     * 点击下单弹出
     */
    function purchase_on_click_fun(sku, su_id) {
        if (sku
            && (sku.replace(/(^\s*)|(\s*$)/g, ""))) {
            var url = "<?php echo url::base(true)?>sirens/purchaseonesku";
            url += "?sku=" + sku;
            url += "?su_id=" + su_id;
            window.open(url);
        }
    }

    /**
     * 点击不下单触发时间
     */
    function nopurchase_on_click_fun(sku) {
        /**
         * 使用array =》 sku
         * 状态 status
         * @type {{}}
         */
        if (sku
            && (sku.replace(/(^\s*)|(\s*$)/g, ""))) {
            var data = [];
            var sku = {"sku": sku, "status": 1};
            data.push(sku);
            $.ajax({
                type: 'post',
                url: '<?php echo url::base(true)?>warning/nowarning_one',
                dataType: 'json',
                data: {"data": data, 'status': 1},
                async: false,
                success: function (data) {
                    if (data) {
                        alert('操作成功');
                        window.location.reload();
                    }
                    else {
                        alert('操作失败');
                    }
                },
                error: function () {
                    alert('error');
                }
            });
        }
    }

    function btn_click_flush(obj) {
        var url = "<?php echo url::base(true)?>warning/flushwarninghandle";
        var dialog = art.dialog({
            title: "更新数据",
            content: '数据更新中...',
            lock: true,
            opacity: 0.87,
            fixed: true,
            drag: false,
            resize: false
        });
        $.get(url, function (data) {
            dialog.content(data);
        })
    }
</script>
<script type="text/javascript">
    function changecss(obj) {
        if ($(obj).hasClass("btn-info")) {
            return;
        } else {
            $("#sel_form a").removeClass("btn-info");
            $(obj).addClass("btn-info");
        }
    }

    function jump(d) {
        var uri = "<?php echo url::base(true);?>sirens?day=" + d + "&sku=" + $("input[name='sku']").val() + "&username=" + $("input[name='username']").val();
        if($("#ck").is("checked")) {
            uri = uri + "&old=1"
        }
        window.location.href = uri;
    }

    function purchase(sku, su_id) {
        if (sku
            && (sku.replace(/(^\s*)|(\s*$)/g, ""))) {
            var url = "<?php echo url::base(true)?>sirens/purchase";
            url += "?sku=" + sku;
            url += "&su_id=" + su_id;
            if($("#ck").is(":checked")){
                url += "&old=1";
            }
            window.open(url);
        }
    }
    function purchase_supplier(sku,supplier,su_id){
        if (sku
            && (sku.replace(/(^\s*)|(\s*$)/g, ""))) {
            var url = "<?php echo url::base(true)?>sirens/purchase_supplier";
            url += "?sku=" + sku;
            url += "&supplier="+supplier;
            url += "&su_id=" + su_id;
            if($("#ck").is(":checked")){
                url += "&old=1";
            }
            window.open(url);
        }
    }
    function download() {
        var params = {};

        $("form input").each(function () {
            params[$(this).attr('name')] = $(this).attr('value');
        });

        params['download'] = 1;

        if($("#ck").is(":checked")){
            params['old'] = 1;
        }

        var past = $("#past").val();
        if(past){
            params['past'] = 1;
        }

        $.get("<?php echo url::base(true);?>/sirens/index", params, function (data) {
            window.open("/sirens/download?md5=" + data);
        });
    }
    //判断单价
    function clearNoNum(obj){   obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符

        obj.value = obj.value.replace(/^\./g,"");  //验证第一个字符是数字而不是.

        obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.

        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    }

    function update_def_ldt(obj, sku, su_id, daysale) {
        var new_ldt = $(obj).val();
        if(new_ldt && new_ldt != $(obj).next().val()) {
            $.ajax({
                type: 'post',
                url: '<?php echo url::base(true)?>sirens/update_ldt',
                dataType: 'json',
                data: {"sku": sku, 'su_id': su_id, 'def_sec':$(obj).val(), 'day_sale':daysale},
                async: false,
                success: function (re) {
                    if (re.ok != 0) {
                        alert('修改成功');
                        $(obj).css("background",'grey');
                        var next_obj = $(obj).parent().next().next().next();
                        next_obj.text(re.best);
                        next_obj.next().text(re.num);
                    }
                    else {
                        alert('未完成修改');
                    }
                },
                error: function () {
                    alert('error');
                }
            });
        }
        return false;
    }

</script>
<div>
    <div>
        <form style="display: inline-block" action="<?php echo url::base(true) ?>sirens/index" method="get"
              id="sel_form">
            <a class="btn <?php if(isset($get['day'])){
                if($get['day']==1){
                    echo "btn-info";
                }
            }else{
                echo "btn-info";
            } ?>" onclick="jump(1)">今日下单</a>
            <a class="btn <?php echo isset($get['day'])&&$get['day']==3?"btn-info":''; ?>" onclick="jump(3)">三日下单</a>
            <a class="btn <?php echo isset($get['day'])&&$get['day']==7?"btn-info":''; ?>" onclick="jump(7)">七日下单</a>
            <input type="hidden" name="day" value="<?php echo isset($get['day'])?$get['day']:'1' ?>">
            <span>sku
                <input type="text" placeholder="" name="sku" class="input-medium" style="width: 90px;"
                       value="<?php if(isset($get['sku'])){
                           echo $get['sku'];
                       } ?>">
                负责人
                <input type="text" name="username" style="width: 110px;"
                       value="<?php echo isset($get['username'])?$get['username']:$opera; ?>">
            </span>
            <span>
                <select id="past" name="past" style="width: 130px">
                    <option value="0">采购支持</option>
                    <option value="1">采购预警</option>
                </select>
            </span>
            <span>老ERP销量
                <input type="checkbox" id="ck" name="old" <?php if(isset($get['old'])&&$get['old']){echo "checked='true'";} ?>>
            </span>
            <button type="submit" class="btn btn-info" id="sel_submit">查询</button>
        </form>
        <button class="btn btn-success" onclick="download()">下载</button>
    </div>
    <div>
        最后更新时间为：<?php echo $last_time; ?>，今日预警<?php echo $lc; ?>个
    </div>
</div>

<div style=" margin-top:20px;">
    <table class="table table-bordered">
        <thead>
        <tr style=" background-color:#4EA392;color:#fff;">
            <th>序号</th>
            <th>SKU</th>
            <th class="show_detail">
                <span style="position: relative">
                    实际库存数量
                    <div class="popover bottom">
                        <div class="arrow"></div>
                        <div class="popover-content" style="color: black">
                            (期初+入-出)
                        </div>
                    </div>
                </span>
            </th>
            <th>总在途数量</th>
            <th>总未发数量</th>
            <th class="show_detail">
                <span style="position: relative">
                    总可用数量
                    <div class="popover bottom">
                        <div class="arrow"></div>
                        <div class="popover-content" style="color: black">
                            (实际库存+总在途-总未发)
                        </div>
                    </div>
                </span>
            </th>
            <th>日均销量</th>
            <th class="show_detail">
                <span style="position: relative">
                    可支持天数
                    <div class="popover bottom">
                        <div class="arrow"></div>
                        <div class="popover-content" style="color: black">
                            (实际库存+总在途-总未发)/日均销量 - 3
                        </div>
                    </div>
                </span>
            </th>
            <th>成本价</th>
            <th>销售状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php if($lists){
            foreach($lists as $sku => $list){
                ?>
                <tr>
                    <td><?php echo $first++; ?></td>
                    <td><?php echo $sku;
                        echo isset($sku_ns[$sku])?':'.$sku_ns[$sku]:''; ?></td>
                    <td><?php echo $list['actual']; ?></td>
                    <td><?php echo $list['onway']; ?></td>
                    <td><?php echo $list['hold']; ?></td>
                    <td><?php echo $list['actual']+$list['onway']-$list['hold']; ?></td>
                    <td><?php echo isset($daysales[$sku])?$daysales[$sku]:0; ?></td>
                    <td><?php echo $list['sup_sec_name']; ?></td>
                    <td><?php echo isset($cost[$sku])?$cost[$sku]:''; ?></td>
                    <td><?php echo isset($sts[$list['sku']])?$sts[$list['sku']]:''; ?></td>
                    <td>
                        <input type="button" class="btn btn-info load" value="展开"/>
                    </td>
                </tr>
                <?php if(isset($sku_sus[$sku])){ ?>
                    <tr style="display: none">
                        <td colspan="11">
                            <table class="table table-hover">
                                <thead>
                                <tr style=" background-color:#0099CC;color:#fff">
                                    <th>供应商</th>
                                    <th>预交期</th>
                                    <th>自定义交期</th>
                                    <th>单价</th>
                                    <th>最近采购时间</th>
                                    <th class="show_detail">
                                        <span style="position: relative">
                                            最佳下单日期
                                            <div class="popover bottom">
                                                <div class="arrow"></div>
                                                <div class="popover-content" style="color: black">
                                                    (可支持天数-交期+当天日期)
                                                </div>
                                            </div>
                                        </span>
                                    </th>
                                    <th class="show_detail">
                                       <span style="position: relative">
                                            建议采购量
                                            <div class="popover bottom">
                                                <div class="arrow"></div>
                                                <div class="popover-content" style="color: black">
                                                    (交期-可支持天数+10)*日均销量
                                                </div>
                                            </div>
                                        </span>
                                    </th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php foreach($sku_sus[$sku] as $su_id => $ldt_sec){ ?>
                                    <tr>
                                        <td><?php echo isset($su_ns[$su_id])?$su_ns[$su_id]:''; ?></td>
                                        <td><?php echo $ldt_sec['ldt_sec_name']; ?></td>
                                        <td>
                                            <input type="text" style="width: 40px;" class="input-medium"
                                                   value="<?php echo $ldt_sec['def_sec_name']; ?>"
                                                   onblur="update_def_ldt(this,'<?php echo $sku;?>',<?php echo $su_id;?>,<?php echo isset($daysales[$sku])?$daysales[$sku]:0;?>)"
                                                   onkeyup="clearNoNum(this)"/>天
                                            <input type="hidden" value="<?php echo $ldt_sec['def_sec_name']; ?>">
                                        </td>
                                        <td><?php echo isset($ps[$sku][$su_id])?$ps[$sku][$su_id]:''; ?></td>
                                        <td><?php echo isset($lp[$sku][$su_id])?$lp[$sku][$su_id]:''; ?></td>
                                        <td><?php echo isset($ldt_sec['best'])?$ldt_sec['best']:''; ?></td>
                                        <td><?php if($daysales[$sku]){
                                                $do = ceil($daysales[$sku]*(($ldt_sec['ldt_sec']-$list['sup_sec'])/(60*60*24)+10));
                                                if($do>0){
                                                    echo ceil($do);
                                                }else{
                                                    echo 0;
                                                }
                                            }else{
                                                echo 0;
                                            } ?></td>
                                        <td>
                                            <input type="button" class="btn btn-success"
                                                   onclick="purchase('<?php echo $sku; ?>', '<?php echo $su_id; ?>')"
                                                   value="单个sku采购"/>
                                            <input type="button" class="btn btn-success"
                                                   onclick="purchase_supplier('<?php echo $sku; ?>','<?php echo isset($su_ns[$su_id])?$su_ns[$su_id]:''; ?>','<?php echo $su_id; ?>')"
                                                   value="供应商采购"/>
                                            <a class="btn btn-success" href="<?php echo url::base(true)?>advancewarning/index?sku=<?php echo strtoupper($sku);?>">采购支持明细</a>
                                            <!--<input type="button" class="btn btn-success" value="忽略"/>-->
                                        </td>
                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                <?php
                }
            }
        } ?>
        </tbody>
    </table>
</div>
<div>
    <?php echo $page; ?>
</div>
<script>
    $(function () {
        $(".load").click(function () {
            $(this).parents('tr').next().toggle();
        });
        $(".show_detail").mouseover(function () {
            $(this).find("div.popover,div.arrow").show();

        }).mouseout(function () {
            $(this).find("div.popover,div.arrow").hide();

        });
    })
</script>

									
							

  
